package com.samoilovich.belarustoday.afisha;

import com.samoilovich.belarustoday.Constants;
import com.samoilovich.belarustoday.R;
import com.samoilovich.belarustoday.afisha.films.CinemaFragment;
import com.samoilovich.belarustoday.afisha.films.FilmDescriptionFragment;
import com.samoilovich.belarustoday.afisha.films.FilmsFragment;
import com.samoilovich.belarustoday.afisha.films.SeancesFragment;
import com.samoilovich.belarustoday.afisha.fragments.AfishaMainFragment;
import com.samoilovich.belarustoday.afisha.fragments.EventDescriptionFragment;
import com.samoilovich.belarustoday.afisha.fragments.EventFragment;
import com.samoilovich.belarustoday.basefragments.BaseFragmentActivity;
import com.samoilovich.belarustoday.calendar.fragments.ICalendarListener;
import com.samoilovich.belarustoday.calendar.fragments.ScheduleCalendarFragment;
import com.samoilovich.belarustoday.datemodel.CinemaModel;
import com.samoilovich.belarustoday.datemodel.EventModel;
import com.samoilovich.belarustoday.datemodel.FilmModel;
import com.samoilovich.belarustoday.tools.Utility;

import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;

import java.util.ArrayList;
import java.util.Calendar;

import hirondelle.date4j.DateTime;

public class AfishaActivity extends BaseFragmentActivity {
    private ScheduleCalendarFragment mDialog;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mUpdateButtonState = true;
        setContentView(R.layout.main);
        Utility.clearBackStack(this);
        Utility.initImageLoader(this);
        setAfishaMainFragment();
    }

    public void setAfishaMainFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content, new AfishaMainFragment(AfishaActivity.this, Constants.AFISHA_MAIN_FRAGMENT_TAG), Constants.AFISHA_MAIN_FRAGMENT_TAG);
        transaction.commit();
    }

    public void setFilmFragment() {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content2, new FilmsFragment(AfishaActivity.this, Constants.FILM_FRAGMENT_TAG), Constants.FILM_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setFilmDescriptionFragment(FilmModel film) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content3, new FilmDescriptionFragment(film), Constants.FILM_DESCRIPTION_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setFilmSeancesFragment(FilmModel film, String date) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content3, new SeancesFragment(AfishaActivity.this, film, date), Constants.SEANCES_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setCinemaFragment(CinemaModel cinema) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content4, new CinemaFragment(cinema), Constants.CINEMA_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setEventFragment(long eventType, String eventName) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content2, new EventFragment(AfishaActivity.this, eventType, eventName), Constants.EVENT_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setEventDescriptionFragment(EventModel event, String eventTypeName) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        transaction.replace(R.id.frameLayout_content3, new EventDescriptionFragment(event, eventTypeName), Constants.EVENT__DESCRIPTION_FRAGMENT_TAG);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void setCalendarFilmsFragment(ICalendarListener listener, DateTime clickedDate) {
        Calendar calendar = Calendar.getInstance();
        mDialog = ScheduleCalendarFragment.newInstance(getString(R.string.choice_of_date), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR));
        mDialog.setCalendarListener(listener);
        mDialog.setSelectedDatesAfterToday();
        mDialog.setClickedDate(clickedDate);
        mDialog.show(getSupportFragmentManager(), Constants.SCHEDULE_CALENDAR_FRAGMENT_TAG);
    }

    public void setCalendarSeancesFragment(ICalendarListener listener, ArrayList<String> days, DateTime clickedDate) {
        Calendar calendar = Calendar.getInstance();
        mDialog = ScheduleCalendarFragment.newInstance(getString(R.string.choice_of_date), calendar.get(Calendar.MONTH) + 1, calendar.get(Calendar.YEAR));
        mDialog.setCalendarListener(listener);
        mDialog.setSelectedDates(days);
        mDialog.setClickedDate(clickedDate);
        mDialog.show(getSupportFragmentManager(), Constants.SCHEDULE_CALENDAR_FRAGMENT_TAG);
    }

    public void dismissCalendarDialog() {
        if (mDialog != null) {
            mDialog.dismiss();
        }
    }

}
